package org.csu.chatserver.service.Impl;

import org.csu.chatserver.mapper.RoomMapper;
import org.csu.chatserver.mapper.RoomMessageMapper;
import org.csu.chatserver.entity.pojo.Room;
import org.csu.chatserver.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RoomMessageMapper roomMessageMapper;


    @Override
    public boolean createRoom(String roomName) {
        Room room = new Room();
        room.setRoomName(roomName);
        return roomMapper.insert(room) > 0;
    }

    @Override
    public Room getRoomById(Integer roomId) {
        return roomMapper.selectById(roomId);
    }

    @Override
    public List<Room> getAllRooms() {
        return roomMapper.selectList(null);
    }

    @Override
    public boolean joinRoom(Integer roomId, String username) {
        Room room = getRoomById(roomId);
        if (room == null) return false;
        
        // 检查是否已在房间中
        if (isUserInRoom(roomId, username)) return false;
        
        // 寻找空位并加入
        if (room.getRoommate1() == null) room.setRoommate1(username);
        else if (room.getRoommate2() == null) room.setRoommate2(username);
        else if (room.getRoommate3() == null) room.setRoommate3(username);
        else if (room.getRoommate4() == null) room.setRoommate4(username);
        else if (room.getRoommate5() == null) room.setRoommate5(username);
        else if (room.getRoommate6() == null) room.setRoommate6(username);
        else return false;

        return roomMapper.updateById(room) > 0;
    }

    @Override
    public boolean leaveRoom(Integer roomId, String username) {
        Room room = getRoomById(roomId);
        if (room == null) return false;

        if (username.equals(room.getRoommate1())) room.setRoommate1(null);
        else if (username.equals(room.getRoommate2())) room.setRoommate2(null);
        else if (username.equals(room.getRoommate3())) room.setRoommate3(null);
        else if (username.equals(room.getRoommate4())) room.setRoommate4(null);
        else if (username.equals(room.getRoommate5())) room.setRoommate5(null);
        else if (username.equals(room.getRoommate6())) room.setRoommate6(null);
        else return false;

        return roomMapper.updateById(room) > 0;
    }

    @Override
    public boolean isUserInRoom(Integer roomId, String username) {
        Room room = getRoomById(roomId);
        if (room == null) return false;

        return username.equals(room.getRoommate1()) ||
               username.equals(room.getRoommate2()) ||
               username.equals(room.getRoommate3()) ||
               username.equals(room.getRoommate4()) ||
               username.equals(room.getRoommate5()) ||
               username.equals(room.getRoommate6());
    }

    @Override
    public List<String> getRoomMembers(Integer roomId) {
        Room room = getRoomById(roomId);
        if (room == null) return null;

        List<String> members = new ArrayList<>();
        if (room.getRoommate1() != null) members.add(room.getRoommate1());
        if (room.getRoommate2() != null) members.add(room.getRoommate2());
        if (room.getRoommate3() != null) members.add(room.getRoommate3());
        if (room.getRoommate4() != null) members.add(room.getRoommate4());
        if (room.getRoommate5() != null) members.add(room.getRoommate5());
        if (room.getRoommate6() != null) members.add(room.getRoommate6());
        
        return members;
    }

    @Override
    public boolean deleteRoom(Integer roomId) {
        // 由于有外键约束的CASCADE设置，删除房间时会自动删除相关消息
        return roomMapper.deleteById(roomId) > 0;
    }
}